package edu.sctu.alex.June.day20150614;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

/**
 * Created by alex on 2015/6/14.
 */
public class IoDemo {
    Scanner scanner = new Scanner(System.in);

    /**
     *
     * 添加目录
     * @param dirStation
     * @return
     * @throws IOException
     */
    public File addDir(String dirStation) throws IOException {

        File file = new File(dirStation);
        if (!file.exists()) {
            file.mkdir();

        }else if(file.exists()){

            System.out.println("目录已存在!");
        }
     return file;
    }

    /**
     *
     * 添加文件
     * @param dirStation
     * @throws IOException
     */
    public void addFile(String dirStation) throws IOException {

        System.out.println("是否想添加新文件?");

        String answer = scanner.next();
        if(answer.equals("是")) {

            System.out.println("请输入你想要创建的文件名和后缀名，例如：abc.txt：");
            String name = scanner.next();
            boolean x = false;
//        File.createTempFile(fileName,fileType,addDir(dirStation));
            File file = new File(dirStation + "/" + "" + name);
            if (!file.exists()) {
                x = file.createNewFile();
            }
            if (x) {
                System.out.println("文件创建成功");
            } else {
                System.out.println("文件创建失败");
            }
        }else {

        }
    }

    /**
     *
     * 删除文件
     * @param dirStation
     */
    public void deleteFile(String dirStation) {
        System.out.println("是否想删除文件？");
        String answer1 = scanner.next();
        if (answer1.equals("是")) {
            File file = new File(dirStation);
            System.out.println("目录：" + dirStation + "下的文件：");
            String[] allFileAndDir;
            allFileAndDir = file.list();
            for (String x : allFileAndDir) {

                System.out.println(x);

            }
            System.out.println("请输入你想要删除的文件：");
            String key = scanner.next();
            File file1 = new File(dirStation + "/" + key);
            System.out.println("是否删除？");

            String answer = scanner.next();
            if (answer.equals("是")) {
                if (file1.delete()) {
                    System.out.println("删除成功!");

                } else {
                    System.out.println("删除失败，文件不存在或输入错误！");

                }

            } else if (answer.equals("否")) {


            }
        }
    }
}
